package com.bdcom.comp;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.ListCellRenderer;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.dialog.RenderDialog;
import com.bdcom.dialog.SignRangeCfgDialog;
import com.bdcom.dialog.WidthHeightValueDialog;
import com.bdcom.listener.CanvasMouseListener;
import com.bdcom.listener.CanvasMouseListener.ListenerCommand;
import com.bdcom.model.BarrierType;
import com.bdcom.model.DeviceType;
import com.bdcom.util.PlannerUtil;

/**
 * 工具栏
 * 
 * @author Administrator
 *
 */
@Component
public class ToolBarPanel extends JMenuBar implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private CanvasMouseListener canvasMouseListener;
	@Autowired
	private CanvasPanel canvasPanel;
	@Autowired
	private RenderDialog renderDialog;
	@Autowired
	private WidthHeightValueDialog widthHeightValueDialog;
	@Autowired
	private SignRangeCfgDialog signRangeCfgDialog;

	private JMenuItem addDeviceMenu = null;

	public static JComboBox<DeviceType> devCombobox = null;

	private JMenuItem addLineMenu = null;

	private JMenuItem addCurveMenu = null;

	private JMenuItem addRectMenu = null;

	private JMenuItem addEllipseMenu = null;

	public static JComboBox<BarrierType> barrierCombobox = null;

	private JMenuItem delMenu = null;

	private JMenuItem setBackImageMenu = null;

	private JMenuItem renderMenu = null;

	private JMenuItem exportMenu = null;

	private JMenuItem resetMenu = null;

	private JMenuItem combinMenu = null;

	private JMenuItem widthHeightMenu = null;

	private JMenuItem signRangeMenu = null;

	private JMenuItem autoLayoutMenu = null;

	public ToolBarPanel() {

		this.setLayout(new FlowLayout(FlowLayout.LEFT));

		this.add(devCombobox = new JComboBox<DeviceType>());

		devCombobox.setPreferredSize(new Dimension(150, 20));

		devCombobox.setEnabled(false);

		devCombobox.setEditable(false);

		devCombobox.setRenderer(new MyDcomboRender());

		this.add(addDeviceMenu = createJmenu(PlannerUtil.getString("add device"), PlannerUtil.CMD_ADD_DEV, "image/button/addDevice.png"));

		JSeparator hs1 = new JSeparator(JSeparator.VERTICAL);

		hs1.setPreferredSize(new Dimension(1, 20));

		hs1.setBackground(Color.BLUE);

		this.add(hs1);

		this.add(barrierCombobox = new JComboBox<BarrierType>());

		barrierCombobox.setPreferredSize(new Dimension(150, 20));

		barrierCombobox.setEnabled(false);

		barrierCombobox.setEditable(false);

		barrierCombobox.setRenderer(new MyBComboRender());

		this.add(addLineMenu = createJmenu(PlannerUtil.getString("line barrier"), PlannerUtil.CMD_ADD_LINE_BARRIER,
				"image/button/addLine.png"));

		this.add(addCurveMenu = createJmenu(PlannerUtil.getString("curve barrier"), PlannerUtil.CMD_ADD_CURVE_BARRIER,
				"image/button/addCurve.png"));

		this.add(addRectMenu = createJmenu(PlannerUtil.getString("rect barrier"), PlannerUtil.CMD_ADD_RECT_BARRIER,
				"image/button/addRect.png"));

		this.add(addEllipseMenu = createJmenu(PlannerUtil.getString("ellipse barrier"), PlannerUtil.CMD_ADD_ELLIPSE_BARRIER,
				"image/button/addEllipse.png"));

		JSeparator hs2 = new JSeparator(JSeparator.VERTICAL);

		hs2.setPreferredSize(new Dimension(1, 20));

		hs2.setBackground(Color.BLUE);

		this.add(hs2);

		this.add(signRangeMenu = createJmenu(PlannerUtil.getString("set sign range"), PlannerUtil.SET_SIGN_RANGE,
				"image/button/signRange.png"));

		this.add(widthHeightMenu = createJmenu(PlannerUtil.getString("set width&height"), PlannerUtil.SET_WIDTH_HEIGHT,
				"image/button/widthheight.png"));

		this.add(autoLayoutMenu = createJmenu(PlannerUtil.getString("auto layout"), PlannerUtil.CMD_AUTO_LAYOUT,
				"image/button/autoLayout.png"));

		this.add(renderMenu = createJmenu(PlannerUtil.getString("render"), PlannerUtil.CMD_RENDER_MAP, "image/button/render.png"));

		this.add(exportMenu = createJmenu(PlannerUtil.getString("export"), PlannerUtil.CMD_EXPORT_MAP, "image/button/export.png"));

		this.add(setBackImageMenu = createJmenu(PlannerUtil.getString("choose background"), PlannerUtil.CMD_SELECT_BACK,
				"image/button/setBackGround.png"));

		this.add(resetMenu = createJmenu(PlannerUtil.getString("reset"), PlannerUtil.CMD_RESET_CANVAS, "image/button/reset.png"));

		this.add(combinMenu = createJmenu(PlannerUtil.getString("combin"), PlannerUtil.CMD_COMBIN_POINTS, "image/button/combine.png"));

		this.add(delMenu = createJmenu(PlannerUtil.getString("delete"), PlannerUtil.CMD_DEL, "image/button/delete.png"));

	}


	private JMenuItem createJmenu(String toolTip, String btnCmd, String imgPath) {

		JMenuItem menu = new JMenuItem();

		menu.setIcon(new ImageIcon(imgPath));

		menu.setToolTipText(toolTip);

		menu.setActionCommand(btnCmd);

		menu.addActionListener(this);

		menu.setEnabled(false);

		return menu;

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		String cmd = e.getActionCommand();

		if (PlannerUtil.CMD_ADD_DEV.equals(cmd)) {
			doAddDeviceCmd();
		} else if (PlannerUtil.CMD_ADD_LINE_BARRIER.equals(cmd)) {
			doAddLineBarrierCmd();
		} else if (PlannerUtil.CMD_ADD_RECT_BARRIER.equals(cmd)) {
			doAddRectBarrierCmd();
		} else if (PlannerUtil.CMD_ADD_ELLIPSE_BARRIER.equals(cmd)) {
			doAddEllipseBarrierCmd();
		} else if (PlannerUtil.CMD_ADD_CURVE_BARRIER.equals(cmd)) {
			doAddCurveBarrierCmd();
		} else if (PlannerUtil.CMD_DEL.equals(cmd)) {
			canvasPanel.removeSelected();
		} else if (PlannerUtil.CMD_SELECT_BACK.equals(cmd)) {
			canvasPanel.chooseBackGroundImage();
		} else if (PlannerUtil.CMD_RESET_CANVAS.equals(cmd)) {
			doRestCanvasCmd();
		} else if (PlannerUtil.CMD_RENDER_MAP.equals(cmd)) {
			doRenderMapCmd();
		} else if (PlannerUtil.CMD_COMBIN_POINTS.equals(cmd)) {
			canvasPanel.combinPoints();
		} else if (PlannerUtil.CMD_EXPORT_MAP.equals(cmd)) {
			doExportCmd();
		} else if (PlannerUtil.SET_WIDTH_HEIGHT.equals(cmd)) {
			widthHeightValueDialog.setVisible(true);
		} else if (PlannerUtil.SET_SIGN_RANGE.equals(cmd)) {
			signRangeCfgDialog.setVisible(true);
		} else if (PlannerUtil.CMD_AUTO_LAYOUT.equals(cmd)) {
			doAutolayoutCmd();
		}
	}

	private void doAutolayoutCmd() {
		// TODO Auto-generated method stub
		if (canvasPanel.getCanvasModel().getMapWidth() == 0) {

			JOptionPane.showMessageDialog(null, PlannerUtil.getString("Set the real width and Height"));

			widthHeightValueDialog.setVisible(true);

			return;
		}
		canvasPanel.autoLayout();
	}

	private void doRestCanvasCmd() {
		// TODO Auto-generated method stub
		int result = JOptionPane.showConfirmDialog(null,
				PlannerUtil.getString("Reset canvas will remove all things in this project."));

		if (result == JOptionPane.YES_OPTION) {
			canvasPanel.resetCanvas();
		}
	}

	private void doExportCmd() {
		// TODO Auto-generated method stub
		/**
		 * 仅仅导出设备列表
		 */
		JFileChooser chooser = new JFileChooser(new File("."));

		chooser.setDialogType(JFileChooser.SAVE_DIALOG);

		chooser.setDialogTitle(PlannerUtil.getString("Export devs' info"));

		FileNameExtensionFilter excelFiles = new FileNameExtensionFilter("excel files(*.xls *.xlsx)", ".xls", ".xlsx");

		chooser.addChoosableFileFilter(excelFiles);

		chooser.setFileFilter(excelFiles);

		if (chooser.showDialog(null, PlannerUtil.getString("Export")) == JFileChooser.APPROVE_OPTION) {

			File oFile;

			if (!chooser.getSelectedFile().getName().endsWith(".xls")
					&& !chooser.getSelectedFile().getName().endsWith(".xlsx")) {

				oFile = new File(chooser.getSelectedFile().getAbsolutePath() + ".xlsx");

			} else {

				oFile = chooser.getSelectedFile();
			}

			if (canvasPanel.exportDevicesAsXls(oFile)) {

				JOptionPane.showMessageDialog(null, PlannerUtil.getString("Export success"));

			} else {

				JOptionPane.showMessageDialog(null, PlannerUtil.getString("Export faild"));

			}
			;

		}
	}

	private void doRenderMapCmd() {
		// TODO Auto-generated method stub
		if (canvasPanel.getCanvasModel().getMapWidth() == 0) {

			JOptionPane.showMessageDialog(null, PlannerUtil.getString("Set the real width and Height"));

			widthHeightValueDialog.setVisible(true);

			return;
		}

		renderDialog.init();
	}

	private void doAddEllipseBarrierCmd() {
		// TODO Auto-generated method stub
		// 判断是否有可用障碍物类型
		if (barrierCombobox.getSelectedItem() == null) {
			JOptionPane.showMessageDialog(null, PlannerUtil.getString("please config barrierType"));
			return;
		}
		// 判断当前的事件，如果不等于添加圆形障碍物事件，就设置当前点击事件为添加圆形障碍物事件，同时其它按钮不能操作，如果等于添加圆形障碍物事件就设置为none。
		if (canvasMouseListener.getListenerCommand() == ListenerCommand.ADDELLIPSE) {
			canvasMouseListener.setListenerCommand(ListenerCommand.NONE);
			addEllipseMenu.setToolTipText(PlannerUtil.getString("ellipse barrier"));
			setEnabled(true);
		} else {
			canvasMouseListener.setListenerCommand(ListenerCommand.ADDELLIPSE);
			setEnabled(false);
			addEllipseMenu.setToolTipText(PlannerUtil.getString("cancel add"));
			addEllipseMenu.setEnabled(true);
		}
	}

	private void doAddRectBarrierCmd() {
		// TODO Auto-generated method stub
		// 判断是否有可用障碍物类型
		if (barrierCombobox.getSelectedItem() == null) {
			JOptionPane.showMessageDialog(null, PlannerUtil.getString("please config barrierType"));
			return;
		}
		// 判断当前的事件，如果不等于添加矩形障碍物事件，就设置当前点击事件为添加矩形障碍物事件，同时其它按钮不能操作，如果等于添加矩形障碍物事件就设置为none。
		if (canvasMouseListener.getListenerCommand() == ListenerCommand.ADDRECT) {
			canvasMouseListener.setListenerCommand(ListenerCommand.NONE);
			addRectMenu.setToolTipText(PlannerUtil.getString("rect barrier"));
			setEnabled(true);
		} else {
			canvasMouseListener.setListenerCommand(ListenerCommand.ADDRECT);
			setEnabled(false);
			addRectMenu.setToolTipText(PlannerUtil.getString("cancel add"));
			addRectMenu.setEnabled(true);
		}
	}

	private void doAddCurveBarrierCmd() {
		// TODO Auto-generated method stub
		// 判断是否有可用障碍物类型
		if (barrierCombobox.getSelectedItem() == null) {
			JOptionPane.showMessageDialog(null, PlannerUtil.getString("please config barrierType"));
			return;
		}
		// 判断当前的事件，如果不等于添加直线障碍物事件，就设置当前点击事件为添加直线障碍物事件，同时其它按钮不能操作，如果等于添加直线障碍物事件就设置为none。
		if (canvasMouseListener.getListenerCommand() == ListenerCommand.ADDCURVE) {
			canvasMouseListener.setListenerCommand(ListenerCommand.NONE);
			addCurveMenu.setToolTipText(PlannerUtil.getString("curve barrier"));
			setEnabled(true);
		} else {
			canvasMouseListener.setListenerCommand(ListenerCommand.ADDCURVE);
			setEnabled(false);
			addCurveMenu.setToolTipText(PlannerUtil.getString("cancel add"));
			addCurveMenu.setEnabled(true);
		}
	}

	private void doAddLineBarrierCmd() {
		// TODO Auto-generated method stub
		// 判断是否有可用障碍物类型
		if (barrierCombobox.getSelectedItem() == null) {
			JOptionPane.showMessageDialog(null, PlannerUtil.getString("please config barrierType"));
			return;
		}
		// 判断当前的事件，如果不等于添加直线障碍物事件，就设置当前点击事件为添加直线障碍物事件，同时其它按钮不能操作，如果等于添加直线障碍物事件就设置为none。
		if (canvasMouseListener.getListenerCommand() == ListenerCommand.ADDLINE) {
			canvasMouseListener.setListenerCommand(ListenerCommand.NONE);
			addLineMenu.setToolTipText(PlannerUtil.getString("line barrier"));
			setEnabled(true);
		} else {
			canvasMouseListener.setListenerCommand(ListenerCommand.ADDLINE);
			setEnabled(false);
			addLineMenu.setToolTipText(PlannerUtil.getString("cancel add"));
			addLineMenu.setEnabled(true);
		}
	}

	private void doAddDeviceCmd() {
		// TODO Auto-generated method stub
		// 判断是否有可用设备类型
		if (devCombobox.getSelectedItem() == null) {
			JOptionPane.showMessageDialog(null, PlannerUtil.getString("please config deviceType"));
			return;
		}
		// 查看当前的事件,如果不等于添加设备事件，就设置点击事件位添加设备事件，同时其它按钮都不能操作。如果等于添加设备事件就设置位none。
		if (canvasMouseListener.getListenerCommand() == ListenerCommand.ADDDEVICE) {
			canvasMouseListener.setListenerCommand(ListenerCommand.NONE);
			// addDeviceMenu.setText(Util.getString("add device"));
			addDeviceMenu.setToolTipText(PlannerUtil.getString("add device"));
			setEnabled(true);
		} else {
			canvasMouseListener.setListenerCommand(ListenerCommand.ADDDEVICE);
			setEnabled(false);
			// addDeviceMenu.setText(Util.getString("cancel"));
			addDeviceMenu.setToolTipText(PlannerUtil.getString("cancel add"));
			addDeviceMenu.setEnabled(true);
		}

	}

	@Override
	public void setEnabled(boolean b) {
		// TODO Auto-generated method stub
		super.setEnabled(b);

		addDeviceMenu.setEnabled(b);

		addLineMenu.setEnabled(b);

		addCurveMenu.setEnabled(b);

		addRectMenu.setEnabled(b);

		addEllipseMenu.setEnabled(b);

		setBackImageMenu.setEnabled(b);

		renderMenu.setEnabled(b);

		exportMenu.setEnabled(b);

		resetMenu.setEnabled(b);

		devCombobox.setEnabled(b);

		barrierCombobox.setEnabled(b);

		widthHeightMenu.setEnabled(b);

		signRangeMenu.setEnabled(b);

		autoLayoutMenu.setEnabled(b);
	}

	// 设置删除按钮是否可用
	public void setEnabledelMenu(boolean b) {
		delMenu.setEnabled(b);
	}

	// 设置合并按钮是否可用
	public void setEnablecombinMenu(boolean b) {
		combinMenu.setEnabled(b);
	}

	// 点击添加设备按钮
	public void clickaddDeviceMenu() {
		// TODO Auto-generated method stub
		addDeviceMenu.doClick();
	}

	// 点击添加障碍物的按钮
	public void clickaddLineMenu() {
		// TODO Auto-generated method stub
		addLineMenu.doClick();
	}

	public void clickaddCurveMenu() {
		// TODO Auto-generated method stub
		addCurveMenu.doClick();
	}

	public void clickaddRectMenu() {
		// TODO Auto-generated method stub
		addRectMenu.doClick();
	}

	public void clickaddEllipseMenu() {
		// TODO Auto-generated method stub
		addEllipseMenu.doClick();
	}

	class MyBComboRender implements ListCellRenderer<Object> {

		JLabel label;

		public MyBComboRender() {
			super();
			// TODO Auto-generated constructor stub

			label = new JLabel();
		}

		@Override
		public java.awt.Component getListCellRendererComponent(JList<?> list, Object value, int index,
				boolean isSelected, boolean cellHasFocus) {
			// TODO Auto-generated method stub

			BarrierType bt = (BarrierType) value;

			if (bt == null) {
				label.setText(PlannerUtil.getString("please config barrierType"));
				return label;
			}

			label.setText(bt.getBarrierName() + " " + bt.getAttenuation());

			label.setBackground(Color.decode(bt.getColor()));

			label.setOpaque(true);

			return label;
		}

	}

	class MyDcomboRender implements ListCellRenderer<Object> {

		JLabel label;

		public MyDcomboRender() {
			super();
			// TODO Auto-generated constructor stub
			label = new JLabel();
		}

		@Override
		public java.awt.Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index,
				boolean isSelected, boolean cellHasFocus) {
			// TODO Auto-generated method stub

			DeviceType dt = (DeviceType) value;

			if (dt == null) {
				label.setText(PlannerUtil.getString("please config deviceType"));
				return label;
			}

			label.setText(dt.getTypeName());

			return label;
		}
	}

}
